package Stack;

import java.util.Iterator;

public class ArrayStack<E> implements StackInterface<E>,Iterable<E>{


    private int top;
    private Object[] arrayStack;

    public ArrayStack() {
    }

    public ArrayStack(int capacity) {
        arrayStack = new Object[capacity];
    }


    @Override
    public boolean push(E value) {
        if (isFull()) {
            return false;
        }
        arrayStack[top++] = value;

        return true;
    }

    @Override
    public E pop() {
        if (isEmpty()) {
            return null;
        }

        return (E)arrayStack[--top];
    }

    @Override
    public E peek() {
        if (top == 0) {
            return  null;
        }
        return (E) arrayStack[top - 1];
    }

    @Override
    public boolean isEmpty() {
        return top == 0;
    }

    @Override
    public boolean isFull() {
        return arrayStack.length == top;
    }

    @Override
    public Iterator<E> iterator() {
        return new Iterator<E>() {
            int p = top;
            @Override
            public boolean hasNext() {
                return p > 0;
            }

            @Override
            public E next() {
                return (E)arrayStack[--p];
            }
        };
    }
}
